package com.aramis.mydytt.ui.search.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.aramis.mydytt.ui.base.BaseFragment
import com.aramis.mydytt.ui.bean.MovieField
import com.aramis.mydytt.ui.search.adapter.SearchTagAdapter

/**
 * Description: SearchTagFragment
 * @author lizhidan
 * Created: 2021/5/6
 */
class SearchTagFragment : BaseFragment() {
    var onSearchTagClickListener: ((cate: MovieField, area: MovieField, year: MovieField, type: MovieField, sort: MovieField) -> Unit)? =
        null
    private val adapters = mutableListOf<SearchTagAdapter>()
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        val linearLayout = LinearLayout(requireContext())
        linearLayout.layoutParams =
            ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
        linearLayout.orientation = LinearLayout.VERTICAL
        initView(linearLayout)
        return linearLayout
    }

    private fun initView(linearLayout: LinearLayout) {
        adapters.clear()
        (0..4).forEach {
            val list = RecyclerView(requireContext())
            val marginLayoutParams =
                ViewGroup.MarginLayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
            list.layoutParams = marginLayoutParams
            marginLayoutParams.setMargins(20, 20, 0, 0)
            list.layoutManager = LinearLayoutManager(requireContext(), LinearLayoutManager.HORIZONTAL, false)
            val searchTagAdapter = SearchTagAdapter(it)
            searchTagAdapter.onItemClickListener = {
                onSearchTagClickListener?.invoke(getField(0), getField(1), getField(2), getField(3), getField(4))
            }
            list.adapter = searchTagAdapter
            adapters.add(searchTagAdapter)
            linearLayout.addView(list)
        }
    }

    private fun getField(style: Int): MovieField {
        return adapters[style].getSelectField()
    }
}